package interview.data_structure;

/**
 * @author fance
 * @date 2018/7/14 15:27
 */

class Node {
    Node next;
    int data;

    public Node(int data) {
        this.data = data;
    }
}
public class MyLinkedList {
    Node head = null;
    public void add(int d) {
        Node node = new Node(d);
        if (head == null) {
            head = node;
            return;
        }
        Node tmp = head;
        while (tmp.next != null) {
            tmp = tmp.next;
        }
        tmp.next = node;
    }

    public boolean del(int index) {
        if (index < 1 || index > length()) {
            return false;
        }
        if (index == 1) {
            head = head.next;
            return true;
        }

        int i = 1;
        Node pre = head;
        Node cur = head.next;
        while (cur != null) {
            if (i == index) {
                pre.next = cur.next;
                return true;
            }
            pre = cur;
            cur = cur.next;
            i++;
        }
        return false;
    }
    public int length() {
        int len = 0;
        Node tmp = head;
        while (tmp.next != null) {
            tmp = tmp.next;
            len++;
        }
        return len;
    }
}
